package person.tavion.demo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil {
    public static String send(String url){
        try {
            URL url2 = new URL(url); // 将URL替换为要连接的网址
            HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(60*1000);
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_OK) { // 检查连接是否成功
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder content = new StringBuilder();

                while ((inputLine = in.readLine()) != null) {
                    content.append(inputLine);
                }

                in.close();
                return content.toString();
            } else {
                return "系统信息：连接失败，响应代码：" + responseCode;
//                System.out.println("连接失败，响应代码：" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}
